<template>
	<view class="_root">
		<!-- #ifdef APP-NVUE -->
		<web-view ref="web" v-if="showWeb" :src="webUrl" :style="'margin-top:-2px;height:' + height + 'px'" />
		<!-- #endif -->
	</view>
</template>
<script>
	export default {
		name: 'uv-read-doc',
		props: {
			// 文档临时路径
			tempFilePath: {
				type: String,
				default: ''
			},
			// 是否允许第三方打开
			otherOpen: {
				type: Boolean,
				default: true
			},
			// 是否允许打印
			print: {
				type: Boolean,
				default: true
			},
			// 是否显示保存按钮
			save: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				showWeb: false,
				// #ifdef APP-NVUE
				height: 10,
				webUrl: '/uni_modules/uv-read-doc/static/app-plus/uv-read-doc/html/web/viewer.html',
				// #endif
			}
		},
		watch: {
			tempFilePath: {
				immediate: true,
				handler(newVal) {
					if (!this.empty(newVal)) {
						let tempFilePath = decodeURIComponent(this.tempFilePath);
						if (tempFilePath.indexOf('http') == -1) {
							tempFilePath = plus.io.convertLocalFileSystemURL(tempFilePath);
						}
						this.webUrl = `${this.webUrl}?file=${encodeURIComponent(tempFilePath)}&isother=${this.otherOpen?1:0}&isprint=${this.print?1:0}&issave=${this.save?1:0}`;
						this.showWeb = true;
					}
				}
			}
		},
		created() {
			this.height = this.sys().windowHeight;
		},
		methods: {
			sys() {
				return uni.getSystemInfoSync()
			},
			empty(value) {
				switch (typeof value) {
					case 'undefined':
						return true
					case 'string':
						if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true
						break
					case 'boolean':
						if (!value) return true
						break
					case 'number':
						if (value === 0 || isNaN(value)) return true
						break
					case 'object':
						if (value === null || value.length === 0) return true
						for (const i in value) {
							return false
						}
						return true
				}
				return false
			}
		}
	}
</script>